const app = getApp();
let page = 0
Page({
  data: {
    bannerList: [{
      title: '测试1',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/469/picture4.jpg'
    },
    {
      title: '测试2',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/4626/25.jpg'
    },
    {
      title: '测试3',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/469/picture4.jpg'
    }
    ],
    bannerTitle: '测试1',
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    col1: [],
    col2: [],
    rewardDetails : []
  },

  /**
   * banner 页面切换监听
   */
  bannerChanged: function (e) {
    let _current = e.detail.current;
    this.setData({
      bannerTitle: this.data.bannerList[_current].title
    });
  },
  /**
   * banner item 点击监听
   */
  bannerItemTap: function (e) {
    wx.showToast({
      icon: 'none',
      title: '点击了第' + e.detail.dataset.index + '个页面',
      duration: 1000
    })
  },
  onLoad: function () {
      wx.getSystemInfo({
        success: (res) => {
          let ww = res.windowWidth;
          let wh = res.windowHeight;
          let imgWidth = ww * 0.48;
          let scrollH = wh;
  
          this.setData({
            scrollH: scrollH,
            imgWidth: imgWidth
          });
          //加载首组图片
          this.loadImages();
        }
      })
    },
  
    onImageLoad: function (e) {
      let imageId = e.currentTarget.id;
      let oImgW = e.detail.width;         //图片原始宽度
      let oImgH = e.detail.height;        //图片原始高度
      let imgWidth = this.data.imgWidth;  //图片设置的宽度
      let scale = imgWidth / oImgW;        //比例计算
      let imgHeight = oImgH * scale;      //自适应高度
  
      let images = this.data.images;
      let imageObj = null;
  
      for (let i = 0; i < images.length; i++) {
        let img = images[i];
        if (img.id === imageId) {
          imageObj = img;
          break;
        }
      }
  
      imageObj.height = imgHeight;
  
      let loadingCount = this.data.loadingCount - 1;
      let col1 = this.data.col1;
      let col2 = this.data.col2;
  
      //判断当前图片添加到左列还是右列
      if (col1.length <= col2.length) {
        col1.push(imageObj);
      } else {
        col2.push(imageObj);
      }
  
      let data = {
        loadingCount: loadingCount,
        col1: col1,
        col2: col2
      };
  
      //当前这组图片已加载完毕，则清空图片临时加载区域的内容
      if (!loadingCount) {
        data.images = [];
      }
      this.setData(data);
    },

    getRewardDetails : function(event){
      var that = this
      var rewardId = event.currentTarget.dataset.rewardid
      wx.navigateTo({
        url: '/pages/reward_details/reward_details?rewardid=' + rewardId
      })
    },
    
    loadImages: function () {
      var that = this
      page++;
      let images = []
      app.getX('reward/reward/list',{'pageNum':page,'pageSize':20},function(res){
          images = res.rows
          let baseId = "img-" + (+new Date());
  
          for (let i = 0; i < images.length; i++) {
            images[i].id = baseId + "-" + i;
          }
      
          that.setData({
            loadingCount: images.length,
            images: images
          });
      })

    }

});
